﻿using MySchool;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm5
{
    public partial class Form2 : Form
    {
        DataSet ds = new DataSet();
        SqlDataAdapter adapter1;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“s1DataSet.Grade”中。您可以根据需要移动或删除它。
            //this.gradeTableAdapter.Fill(this.s1DataSet.Grade);
            // TODO: 这行代码将数据加载到表“s1DataSet.Grade”中。您可以根据需要移动或删除它。
            // 年级下拉列表初始化
            DBHelper dBHelper = new DBHelper();
            dBHelper.OpenConnection();
            string sql = "select * from Grade";
            SqlDataAdapter adapter = new SqlDataAdapter(sql, dBHelper.Connection);
            adapter.Fill(ds, "Grade");

            // 添加一个全部选项   4行代码

            this.comboBox1.DataSource = ds.Tables["Grade"];
            this.comboBox1.ValueMember = "Id";
            this.comboBox1.DisplayMember = "gname";

            this.dataGridView1.AutoGenerateColumns = false;
            // dataGridView 初始化
            string sql1 = "select * from sys_user";
            adapter1 = new SqlDataAdapter(sql1, dBHelper.Connection);
            adapter1.Fill(ds, "sys_user");
            this.dataGridView1.DataSource = ds.Tables["sys_user"];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DBHelper dBHelper = new DBHelper();
            // 清空dataGridView
            ds.Tables["sys_user"].Clear();
            // 根据条件查询符合的数据
            int gId = Convert.ToInt32(this.comboBox1.SelectedValue);
            string sql1 = "select * from sys_user where g_id = " + gId;
            adapter1 = new SqlDataAdapter(sql1, dBHelper.Connection);
            adapter1.Fill(ds, "sys_user");
            this.dataGridView1.DataSource = ds.Tables["sys_user"];
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder builder = new SqlCommandBuilder(adapter1);
            adapter1.Update(ds, "sys_user");
        }
    }
}
